package com.example.goodsmanagementcabinet.serialport;

import android.text.TextUtils;
import android.util.Log;

import com.aill.androidserialport.SerialPort;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class LockStateSerialPortConnect {
    public static LockStateSerialPortConnect serialPortConnect;
    private String strReturnCode = "A55A0A";
    private FileOutputStream mOutputStream;
    private FileInputStream mInputStream;
    private SerialPort sp;
    private ReadThread mReadThread;
    private StringBuffer recevierType = new StringBuffer();
    private boolean isReadData = false;

    public static LockStateSerialPortConnect getInstance() {
        if (serialPortConnect == null) {
            serialPortConnect = new LockStateSerialPortConnect();
        }
        return serialPortConnect;
    }

    public boolean connect(String device, int baud) {
        boolean con = false;
        try {
            sp = new SerialPort(new File("" + device), baud, 0);//这里写你要打开的串口和波特率
            Log.e("SerialPortConnect", "打开串口成功");
            mOutputStream = (FileOutputStream) sp.getOutputStream();
            mInputStream = (FileInputStream) sp.getInputStream();
            con = true;
            receviedData();
        } catch (Throwable tr) {
            con = false;
            Log.e("SerialPortConnect", "打开串口失败");
        }
        return con;
    }

    public void receviedData() {
        if (mReadThread == null) {
            mReadThread = new ReadThread();
            isReadData = true;
            mReadThread.start();
        }
    }

    private class ReadThread extends Thread {//接收线程
        byte[] buffer = new byte[14];

        @Override
        public void run() {
            super.run();
            while (isReadData) {
                int size;
                try {
                    if (mInputStream != null) {
                        size = mInputStream.read(buffer);//接收数据
                        if (size > 0) {
                            onDataReceived(buffer, size);
                            String receveData = bytesToHexString(buffer, size);
                            Log.e("LockSerialPortConnect", "receveData: " + receveData);
                            if (receveData.substring(0, 6).equals(strReturnCode)) {
                                if (TextUtils.equals(receveData.substring(6, 8), "01")) {
                                } else if (TextUtils.equals(receveData.substring(6, 8), "02")) {
                                } else if (TextUtils.equals(receveData.substring(6, 8), "03")){
                                }
                            }
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    return;
                }
            }
        }
    }

    public static String bytesToHexString(byte[] bArray, int length) {
        StringBuffer sb = new StringBuffer(length);
        String sTemp;
        for (int i = 0; i < length; i++) {
            sTemp = Integer.toHexString(0xFF & bArray[i]);
            if (sTemp.length() < 2)
                sb.append(0);
            sb.append(sTemp.toUpperCase());
        }
        return sb.toString();
    }

    protected void onDataReceived(final byte[] buffer, final int size) {
        new Thread(new Runnable() {
            public void run() {
                recevierType.append(new String(buffer, 0, size));
            }
        });
    }
}